package com.taxi.driver.module.main.mine.help.feedback;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Selection;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.taxi.driver.R;
import com.taxi.driver.common.Application;
import com.taxi.driver.common.BaseActivity;
import com.taxi.driver.module.main.mine.help.feedback.dagger.DaggerFeedbackComponent;
import com.taxi.driver.module.main.mine.help.feedback.dagger.FeedbackModule;
import com.taxi.driver.module.vo.FeedbackVO;
import com.taxi.driver.util.EmojiFilter;
import com.taxi.driver.widget.XScrollView;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 建议反馈
 */
public class FeedbackActivity extends BaseActivity implements FeedbackContract.View, TextWatcher,
        SwipeRefreshLayout.OnRefreshListener, XScrollView.XScrollViewListener {
    @BindView(R.id.swipe_refresh_layout)
    SwipeRefreshLayout swipe_refresh_layout;
    @BindView(R.id.scroll_view)
    XScrollView scroll_view;
    @BindView(R.id.et_content)
    EditText et_content;
    @BindView(R.id.tv_account)
    TextView tv_account;
    @BindView(R.id.btn_push)
    Button btn_push;
    @BindView(R.id.recycler_view)
    RecyclerView recycler_view;

    @Inject
    FeedbackPresenter mPresenter;
    FeedbackAdapter adapter;
    LinearLayoutManager linearLayoutManager;

    final int REFRESH = 0;
    final int LOADMORE = 1;
    int MODE = REFRESH;
    int nowPage = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);
        ButterKnife.bind(this);
        initView();

        DaggerFeedbackComponent.builder()
                .appComponent(Application.getAppComponent())
                .feedbackModule(new FeedbackModule(this))
                .build().inject(this);

        mPresenter.reqFeedbacks(nowPage);
    }

    private void initView() {
        swipe_refresh_layout.setOnRefreshListener(this);
        scroll_view.setXScrollViewListener(this);
        et_content.addTextChangedListener(this);
        et_content.setFilters(new InputFilter[]{new EmojiFilter()});
        linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycler_view.setLayoutManager(linearLayoutManager);
        adapter = new FeedbackAdapter(this);
        recycler_view.setAdapter(adapter);
        recycler_view.setNestedScrollingEnabled(false);
        et_content.setHint(R.string.feedback_hint);
//        if (BuildConfig.FLAVOR_type.equals("taxi")) {
//            et_content.setHint(R.string.feedback_hint);
//        } else {
//
//        }
    }

    @OnClick({R.id.btn_push})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_push:
                if (TextUtils.isEmpty(et_content.getText().toString().trim())) {
                    toast("反馈内容不能为空");
                    return;
                }
                mPresenter.addFeedback(et_content.getText().toString());
                break;
        }
    }

    @Override
    public boolean isActive() {
        return false;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        Editable editable = et_content.getText();
        int len = editable.length();

        tv_account.setText(len + "/100");

        if (len <= 0) {
            btn_push.setBackgroundResource(R.drawable.dra_dark_btn_cant_press);
            btn_push.setEnabled(false);
        } else {
            btn_push.setBackgroundResource(R.drawable.sel_dark_btn);
            btn_push.setEnabled(true);
        }

        if (len >= 100) {
            tv_account.setTextColor(getResources().getColor(R.color.red));
        } else {
            tv_account.setTextColor(getResources().getColor(R.color.text_6b7886));
        }

        if (len > 100) {
            int selEndIndex = Selection.getSelectionEnd(editable);
            String str = editable.toString();
            //截取新字符串
            String newStr = str.substring(0, 100);
            et_content.setText(newStr);
            editable = et_content.getText();

            //新字符串的长度
            int newLen = editable.length();
            //旧光标位置超过字符串长度
            if (selEndIndex > newLen) {
                selEndIndex = editable.length();
            }
            //设置新光标所在的位置
            Selection.setSelection(editable, selEndIndex);

        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    @Override
    public void showFeedbacks(List<FeedbackVO> feedbacks) {
        swipe_refresh_layout.setRefreshing(false);
        scroll_view.stopLoadMore();

        if (feedbacks != null && feedbacks.size() > 0) {
            if (MODE == REFRESH)
                adapter.clear();
            adapter.addAll(feedbacks);
        } else {
            scroll_view.showNoMoreHint();
        }
    }

    @Override
    public void addFeedbackSucc() {
        toast(getResources().getString(R.string.push_success));
        et_content.setText("");
        adapter.clear();
        nowPage = 1;
        mPresenter.reqFeedbacks(nowPage);
    }

    @Override
    public void addFeedbackFail(int errorCode, String errorMsg) {
        toast(errorMsg);
    }

    @Override
    public void onRefresh() {
        MODE = REFRESH;
        nowPage = 1;
        mPresenter.reqFeedbacks(nowPage);
    }

    @Override
    public void onLoadMore() {
        MODE = LOADMORE;
        nowPage++;
        mPresenter.reqFeedbacks(nowPage);
    }
}
